<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 员工管理
 * @package app\Staff
 * @author LJQ<245369209@qq.com>
 */
class Staff extends Api

{

    /**
   * 员工列表
   * @param string enterpriseId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function staffList($scope = null){
		$where = [] ;
		if($scope){
			
			$where['enterpriseId'] = $scope;
		}
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//员工列表
			$info =collection(model('Staff')->with('enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//员工列表
			$info =collection(model('Staff')->with('enterprise')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Staff')->where($where)->count('id'); 
		
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到员工','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'员工信息','count'=>$count,'data'=>$info]);
       		}
            	return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
       
   	  
   	 //添加员工
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Staff');
          $result = $model->change($data);
          if (false != $result) {
             setlog('Staffadd','Staff',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'员工添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'添加失败,请检查']);
          }
      }

   	

   /**
   * 修改员工
   * @param  string StaffId 员工id,
   * @return Json
   */
   	public function edit($data = null, $staffId = null){
        
          if (empty($staffId)) {
              return json(['code'=>0,'msg'=>'员工不存在,请检查']);
          }

          $model = model('Basic/Staff');     
          if ($model->change($data)) {
            setlog('Staffupdate','Staff',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'员工信息修改成功']);
          }else{
             return json(['code'=>0,'msg'=>'员工信息修改失败,请检查']);
          }
    }
  
    /**
   *  删除员工
   * @param string StaffId 员工id,
   * @param Json
   */
     
    public function del($staffId = null) {
      	$staffId = input('StaffId',0,'intval');
            $model = model('Basic/Staff');
         	if (empty ( $staffId )||empty(($model->where(array('id'=>$staffId))->find()))) {
           	return  json(['code'=>0,'msg'=>'员工id不存在,请检查']);
        }
        if($model->destroy(['id'=>$staffId]) !== FALSE){
          setlog('Staffdel','Staff',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'员工删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'员工删除失败,请检查']);
       }
    }

     
   /**
   *   查询员工
   * @param string keyword 员工名称
   * @param string enterpriseId企业ID
   * @param string  post 职位ID
   * @return Json
   */
   	public function search($keyword = null,$enterpriseId = null,$post = null){
   		$where = [] ;

		if($enterpriseId){
			$where['enterpriseId'] = ['eq',$enterpriseId];
		}

		if($post){
			$where['post'] = ['eq',$post];
		}

          if($keyword){
                 $where['staffName'] = ['like','%'.$keyword.'%'];
            }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//员工列表
			$info =collection(model('Staff')->with('enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//员工列表
			$info =collection(model('Staff')->with('enterprise')->where($where)->select())->toArray();
		}
	 	//总数
	    $count = model('Staff')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
         	return json(['code'=>0,'msg'=>'没有找到员工','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'员工信息','count'=>$count,'data'=>$info]);
       	}
      }
}